const enterpriseVue = new Vue({
  el: '#app',
  data() {
    //校验身份证号
    var checkIDCard = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('身份证号不能为空'));
      }
      setTimeout(() => {
        if (!ID_Validity(value)) {
          callback(new Error('请输入正确的身份证号'));
        }
      }, 100);
    }
    return {
      pickerOptions: pickerOptions,
      rules: {
        Supplier_SerialNumber: [{ required: true, message: '请选择供应商', trigger: 'change' }],
        Purchase_Date: [{ required: true, message: '请输入采购日期', trigger: 'blur' }],
        Purchase_Amount: [{ required: true, message: '请输入订单金额', trigger: 'blur' }],
        Store_SerialNumber: [{ required: true, message: '请选择仓库', trigger: 'change' }],
        Quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        Purchase_UnitPrice: [{ required: true, message: '请输入成交单价', trigger: 'blur' }],
      },
      form: {
        Purchase_Order_SerialNumber: '',
        Supplier_SerialNumber: '',
        Purchase_Date: '',
        Purchase_Type: '2',
        Purchase_Amount: 0,
        Paid_Amount: 0,
        Return_Amount: 0,
        Apply_Amount: 0,
        Confirm_Amount: 0,
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Row_Status: 5,
        Pay_Type: '0',
        Children: {
          purchase_order_detail: [{       //采购订单明细
            Purchase_Order_Detail_SerialNumber: "",
            Purchase_Order_SerialNumber: '',
            Unit_Price_SerialNumber: '',//价格单主键
            Brand_SerialNumber: '',
            Article_SerialNumber: '',//商品主键
            Article_Specifications_SerialNumber: '',//规格主键
            Article_UnitPrice: '',
            Purchase_UnitPrice: '',
            Quantity: '',
            Store_SerialNumber: "",
            Depot_Type: 0,
            Receipt_Quantity: 0,
            Not_Receipt_Quantity: 0,
            Shippment_Quantity: 0,
            Inventory_Quantity: 0,
            Amount: 0,
            article: [],
            specs: [],
          }],
          purchase_order_paystages: [{
            Purchase_Order_Paystages_SerialNumber: '',
            Purchase_Order_SerialNumber: '',
            Trading_Explain: '',
            Trading_Detail_SerialNumber: '',
            Trading_Percentage: '',
            Trading_Days: '',
            Purchase_Amount: '',
            Paid_Amount: '',
            Collection_Date: '',
            Confirm: '',
          }]
        }
      },
      Wares: [],
      supplier_info: [],
      brand: [],
      PaymentDetail:[],
      isPaymentDetail:[],
      loading: false
    }
  },
  mounted() {
    console.log(this.form.Supplier_SerialNumber == '' || this.brand.length == 0);
    this.getData()
    this.getWares()
    this.getPaymentDetail()
    console.log(this.form.Supplier_SerialNumber != '' && this.brand.length != 0);
  },
  methods: {
    getWares() {//获取所有可选仓库
      let this_ = this
      $.ajax({
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
        type: 'POST',
        data: {
          Data: 'warehouse_info',
        },
        success: function (res) {
          res.forEach(item => {
            this_.Wares.push({
              label: item.Warehouse_Name,
              value: item.Applicable_Type == '0' ? item.Warehouse_SerialNumber : item.Parent_Warehouse_SerialNumber
            })
          })
          console.log(this_.Wares)
        },
        dataType: "json"
      });
    },
    getData() {
      const _this = this
      $.ajax({
        url: `${DomainName}/getInquiryAction?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Data: 'supplier_info'
        },
        success: function (res) {
          console.log(res)
          _this.supplier_info = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getBrand() {//查询供应商经营品牌
      $.ajax({
        type: 'POST',
        url: DomainName + "/getSupplier_Brand?token=" + $.cookie("token"),
        data: {
          Supplier_SerialNumber: this.form.Supplier_SerialNumber,
        },
        success: (res) => {
          this.brand = res.Data
        },
        complete: (XMLHttpRequest) => {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    getArticle(e, id) {
      this.form.Children.purchase_order_detail[id].Article_SerialNumber = ''
      this.form.Children.purchase_order_detail[id].Article_Specifications_SerialNumber = ''
      this.form.Children.purchase_order_detail[id].specs = []
      this.form.Children.purchase_order_detail[id].Article_UnitPrice = ''
      this.form.Children.purchase_order_detail[id].Purchase_UnitPrice = ''
      this.form.Children.purchase_order_detail[id].Quantity = ''
      this.form.Children.purchase_order_detail[id].Amount = ''
      this.count(id)
      const _this = this
      $.ajax({
        url: `${DomainName}/getInquiryIDAction?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Data: 'article_info',
          Primary_Key_Name: 'Brand_SerialNumber',
          Primary_Key: e
        },
        success: function (res) {
          _this.form.Children.purchase_order_detail[id].article = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    Change_Specifications_Name(index) {
      let str = this.form.Children.purchase_order_detail[index].Article_Specifications_SerialNumber
      console.log(str)
      console.log(this.form.Children.purchase_order_detail[index].specs[0])
      let arr = this.form.Children.purchase_order_detail[index].article
      arr.forEach(item => {
        if (item.Article_SerialNumber == this.form.Children.purchase_order_detail[index].Article_SerialNumber) {
          this.form.Children.purchase_order_detail[index].Article_UnitPrice = item.Unit_Price
        }
      })
    },
    getUnitPrice(e, id) {
      //吊牌价
      //查询规格
      this.form.Children.purchase_order_detail[id].Article_Specifications_SerialNumber = ''
      console.log(e);
      $.ajax({
        type: 'POST',
        url: DomainName + "/SpecificationsIDAction?token=" + $.cookie("token"),
        data: {
          Article_SerialNumber: e,
        },
        success: (res) => {
          this.form.Children.purchase_order_detail[id].specs = res
          this.form.Children.purchase_order_detail[id].isDisabled = false
        },
        complete: (XMLHttpRequest) => {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    count(id) {
      if (this.form.Children.purchase_order_detail[id].Purchase_UnitPrice != '' && this.form.Children.purchase_order_detail[id].Quantity != '') {
        this.form.Children.purchase_order_detail[id].Amount = accMul(this.form.Children.purchase_order_detail[id].Purchase_UnitPrice, this.form.Children.purchase_order_detail[id].Quantity)
        this.form.Purchase_Amount = 0
        this.form.Children.purchase_order_detail.forEach(item => {
          this.form.Purchase_Amount = accAdd(this.form.Purchase_Amount, item.Amount)
        })
      } else {
        this.form.Children.purchase_order_detail[id].Amount = ''
        this.form.Purchase_Amount = 0
        this.form.Children.purchase_order_detail.forEach(item => {
          this.form.Purchase_Amount = accAdd(this.form.Purchase_Amount, item.Amount)
        })
      }
    },
    changePay(e) {
      if (e == '0') {
        this.form.Children.purchase_order_paystages = [{
          Purchase_Order_Paystages_SerialNumber: '',
          Purchase_Order_SerialNumber: '',
          Trading_Explain: '全款',
          Trading_Detail_SerialNumber: 'T01000000000001',
          Trading_Percentage: '',
          Trading_Days: '',
          Purchase_Amount: '',
          Paid_Amount: '',
          Collection_Date: '',
          Confirm: '',
        }]
      } else if (e == '1') {
        this.form.Children.purchase_order_paystages= [{
          Purchase_Order_Paystages_SerialNumber: '',
          Purchase_Order_SerialNumber: '',
          Trading_Explain: '',
          Trading_Detail_SerialNumber: '',
          Trading_Percentage: '',
          Trading_Days: '',
          Purchase_Amount: '',
          Paid_Amount: '',
          Collection_Date: '',
          Confirm: '',
        }]
      }
    },
    ChangePaymentDetail(e,i,item){
      if(this.form.Pay_Type == '0'){
        //1.将选中的对象存进数组
      
        // item.Trading_Explain = this.PaymentDetail.filter(item=>item)



      }else if (this.form.Pay_Type == '1') {
  
        


      }
      console.log(e);
      console.log(i);
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.form.Children.purchase_order_paystages = [{
            Purchase_Order_Paystages_SerialNumber: "",
            Purchase_Order_SerialNumber: "",
            Trading_Explain: "",
            Trading_Detail_SerialNumber: "",
            Trading_Percentage: "",
            Trading_Days: "",
            Purchase_Amount: _this.form.Purchase_Amount,
            Paid_Amount: 0,
            Collection_Date: "",
          }];
          _this.loading = true
          $.ajax({
            url: DomainName + '/setPurchaseOrderAction?token=' + $.cookie("token"),
            type: 'POST',
            data: {
              Data: JSON.stringify({
                purchase_order: [_this.form]
              }),
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './PurchaseOrder.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    getPaymentDetail() {
      let _this = this;
      $.ajax({
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
        type: 'POST',
        data: {
          Data: 'trading_detail',
          isNullEnterprise: true,
        },
        success: function (res) {
          _this.PaymentDetail = res;
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add_purchase_order_detail() {
      this.form.Children.purchase_order_detail.push({
        Purchase_Order_Detail_SerialNumber: "",
        Purchase_Order_SerialNumber: '',
        Brand_SerialNumber: '',
        Article_SerialNumber: '',
        Article_UnitPrice: '',
        Purchase_UnitPrice: '',
        Quantity: '',
        Amount: '',
        Store_SerialNumber: "",
        Depot_Type: 0,
        article: [],
        specs: [],
        purchase_order_paystages: [{

        }]
      })
    },
    remove_purchase_order_detail() {
      if (this.form.Children.purchase_order_detail.length > 1) {
        this.form.Children.purchase_order_detail.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_purchase_order_paystages() {
      this.form.Children.purchase_order_paystages.push({
        Purchase_Order_Paystages_SerialNumber: '',
        Purchase_Order_SerialNumber: '',
        Trading_Explain: '',
        Trading_Detail_SerialNumber: '',
        Trading_Percentage: '',
        Trading_Days: '',
        Purchase_Amount: '',
        Paid_Amount: '',
        Collection_Date: '',
        Confirm: '',
      })
    },
    remove_purchase_order_paystages() {

    },
    Pricethousands(num) {//转千分位
      return thousands(num)
    },
  }
})